<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>星座运势查询</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f7fa;
        }
        .container {
            text-align: center;
            margin-top: 50px;
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            margin-bottom: 30px;
        }
        .select-container {
            margin-bottom: 20px;
        }
        select {
            padding: 10px 15px;
            width: 250px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }
        button {
            padding: 10px 20px;
            background-color: #4285f4;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin-left: 10px;
        }
        button:hover {
            background-color: #3367d6;
        }
        .result {
            margin-top: 30px;
            padding: 20px;
            border: 1px solid #eee;
            border-radius: 8px;
            display: none;
            text-align: left;
        }
        .result h3 {
            color: #4285f4;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
            margin-top: 0;
        }
        .fortune-item {
            margin: 10px 0;
            line-height: 1.6;
        }
        .error {
            color: #ea4335;
            margin-top: 20px;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>星座今日运势查询</h1>
        <div class="select-container">
            <select id="constellationSelect">
                <option value="">请选择星座</option>
                <option value="白羊座">白羊座</option>
                <option value="金牛座">金牛座</option>
                <option value="双子座">双子座</option>
                <option value="巨蟹座">巨蟹座</option>
                <option value="狮子座">狮子座</option>
                <option value="处女座">处女座</option>
                <option value="天秤座">天秤座</option>
                <option value="天蝎座">天蝎座</option>
                <option value="射手座">射手座</option>
                <option value="摩羯座">摩羯座</option>
                <option value="水瓶座">水瓶座</option>
                <option value="双鱼座">双鱼座</option>
            </select>
            <button onclick="queryFortune()">查询运势</button>
        </div>
        <div class="result" id="fortuneResult"></div>
        <div class="error" id="errorMsg"></div>
    </div>

    <script>
        // 替换为你在聚合数据申请的API密钥（key）
        const API_KEY = "你的聚合数据密钥";
        const API_URL = "http://web.juhe.cn/constellation/getAll";

        function queryFortune() {
            const constellationSelect = document.getElementById("constellationSelect");
            const constellation = constellationSelect.value;
            const resultDiv = document.getElementById("fortuneResult");
            const errorDiv = document.getElementById("errorMsg");

            // 清空之前的结果和错误信息
            resultDiv.style.display = "none";
            errorDiv.textContent = "";

            // 验证输入
            if (!constellation) {
                errorDiv.textContent = "请选择星座";
                return;
            }


            const xhr = new XMLHttpRequest();


            const params = `consName=${encodeURIComponent(constellation)}&type=today&key=${API_KEY}`;
            const url = `${API_URL}?${params}`;
            xhr.open("GET", url, true);
            xhr.responseType = "json";

       
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        const data = xhr.response;
        
                        if (data.error_code === 0) {

                            resultDiv.innerHTML = `
                                <h3>${data.name} 今日运势（${data.datetime}）</h3>
                                <div class="fortune-item"><strong>综合运势：</strong>${data.all}分</div>
                                <div class="fortune-item"><strong>爱情运势：</strong>${data.love}</div>
                                <div class="fortune-item"><strong>事业运势：</strong>${data.work}</div>
                                <div class="fortune-item"><strong>财富运势：</strong>${data.money}</div>
                                <div class="fortune-item"><strong>健康运势：</strong>${data.health}</div>
                                <div class="fortune-item"><strong>今日幸运色：</strong>${data.color}</div>
                                <div class="fortune-item"><strong>今日幸运数字：</strong>${data.number}</div>
                                <div class="fortune-item"><strong>今日速配星座：</strong>${data.QFriend}</div>
                                <div class="fortune-item"><strong>运势概述：</strong>${data.summary}</div>
                            `;
                            resultDiv.style.display = "block";
                        } else {
                            // 接口返回错误信息（如密钥错误、参数错误等）
                            errorDiv.textContent = `查询失败：${data.reason}（错误码：${data.error_code}）`;
                        }
                    } 
                }
            };
            // 发送请求
            xhr.send();
        }
    </script>
</body>
</html>